package com.piece.admin.service;

import com.piece.admin.model.TbSysUserOauth;
import com.piece.admin.repository.TbSysUserOauthRepository;
import com.piece.core.jpa.repository.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TbSysUserOauthService extends BaseService<TbSysUserOauth, String> {

    @Autowired
    private TbSysUserOauthRepository tbSysUserOauthRepository;

    @PostConstruct
    public void initRepository() {
        setRepository(tbSysUserOauthRepository);
    }

    public boolean checkOpenIdWithType(String type, String openId) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("oauthType", type);
        condition.put("openid", openId);
        TbSysUserOauth oauth = tbSysUserOauthRepository.findOneByCondition(condition);
        return null == oauth ? false : true;
    }

    public TbSysUserOauth getOauthByOpenIdWithType(String type, String openId) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("oauthType", type);
        condition.put("openid", openId);
        return tbSysUserOauthRepository.findOneByCondition(condition);
    }

    public List<TbSysUserOauth> getOauthByUid(long userId) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("userId", userId);
        return tbSysUserOauthRepository.findByCondition(condition, null);
    }
}
